package com.lzm.ydpt.module.mall.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.byt.framlib.commonutils.image.ImageLoaderUtils;
import com.byt.framlib.commonwidget.OnNoDoubleClickListener;
import com.lzm.ydpt.R;
import com.lzm.ydpt.entity.mall.ProductBean;

import java.util.List;

public class ProductListAdapter extends RecyclerView.Adapter<ProductListAdapter.ViewHolder> {


    private List<ProductBean> listData;
    private Activity mActivity;
    private OnItemClickListener onItemClickListener;

    public ProductListAdapter(List<ProductBean> listData, Activity mActivity) {
        this.listData = listData;
        this.mActivity = mActivity;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, ProductBean data, int position);
    }

    public void setItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_product_grid_layout, parent, false));
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ProductBean item = listData.get(position);
        ImageLoaderUtils.display(holder.img_product_pic, item.getImage_src());
        holder.tv_product_name.setText(item.getTitle());
        holder.tv_product_desc.setText(item.getFunctional_indications());
        holder.tv_product_sell_price.setText(item.getMin_price() + "");
        holder.tv_product_sales.setText("已售300");
        holder.tv_product_sell.setText(item.getStore_name());
        holder.itemView.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, listData.get(position), position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return listData.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView img_product_pic;
        TextView tv_product_name;
        TextView tv_product_desc;
        TextView tv_product_sell_price;
        TextView tv_product_sales;
        TextView tv_product_sell;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img_product_pic = itemView.findViewById(R.id.img_product_pic);
            tv_product_name = itemView.findViewById(R.id.tv_product_name);
            tv_product_desc = itemView.findViewById(R.id.tv_product_desc);
            tv_product_sell_price = itemView.findViewById(R.id.tv_product_sell_price);
            tv_product_sales = itemView.findViewById(R.id.tv_product_sales);
            tv_product_sell = itemView.findViewById(R.id.tv_product_sell);
        }
    }
}